package com.erpanderp.wam.tm.model.service;

import java.util.List;

import org.hibernate.FlushMode;
import org.hibernate.Query;
import org.hibernate.Session;

import com.erpanderp.wam.tm.model.entity.RRFOfferLetterHRInputs;
import com.erpanderp.wam.usm.model.entity.user.Employee;
import com.erpanderp.wam.util.HibernateUtil;

public class RRFOfferLetterHRInputsDAO {
	
	 public RRFOfferLetterHRInputs saveRRFOfferLetterHRInputs(RRFOfferLetterHRInputs rrfOfferLetterHRInputs){
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.getTransaction().begin();			
			session.setFlushMode(FlushMode.COMMIT);
			session.saveOrUpdate(rrfOfferLetterHRInputs);
			session.getTransaction().commit();
			return rrfOfferLetterHRInputs;
			}catch(Exception e){
				e.printStackTrace();
				return null;
			}		
		}
	 public boolean isHRInputExistOrNot(String rrfCode,Long profileId) {
			boolean isHRInputExist = true;
			try {
				Session session = HibernateUtil.getSessionFactory()
						.getCurrentSession();
				session.getTransaction().begin();
				String queryString = "from RRFOfferLetterHRInputs hi where hi.recruitmentRequestCode=:rrfCode and hi.recruitmentProfileId.recruitmentProfileId =:profileId";
				Query query = session.createQuery(queryString).setString(
						"rrfCode", rrfCode).setLong("profileId",profileId);
				query.setCacheable(false);

				List tempList = query.list();

				if (tempList == null || tempList.size() == 0) {
					isHRInputExist = false;
				}

				session.close();
				return isHRInputExist;
			} catch (Exception e) {
				e.printStackTrace();
				return isHRInputExist;
			}
		}
	 
	 public RRFOfferLetterHRInputs getRRFOfferLetterHRInputsByProfileId(String rrfCode,Long profileId){ 
		 RRFOfferLetterHRInputs rrfOfferLetterHRInput ;		
			try{
				Session session = HibernateUtil.getSessionFactory().getCurrentSession();
				session.beginTransaction();
				session.setFlushMode(FlushMode.COMMIT);
				String queryString = "from RRFOfferLetterHRInputs hi where hi.recruitmentRequestCode=:rrfCode and hi.recruitmentProfileId.recruitmentProfileId =:profileId";
				
				Query query = session.createQuery(queryString);
				query.setCacheable(false).setString("rrfCode", rrfCode).setLong("profileId", profileId);			
				rrfOfferLetterHRInput = (RRFOfferLetterHRInputs) query.uniqueResult();
				session.getTransaction().commit();
				
				return rrfOfferLetterHRInput;
				
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}	
		}
	 
	 
	 public static void main(String a[]){
		 System.out.println(new RRFOfferLetterHRInputsDAO().isHRInputExistOrNot("E50002-RRF-1", 2l));
	 }
}
